/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientsSide;

import Message.Message;
import Message.Message.invokesStore;
import genclass.GenericIO;

/**
 *
 * @author Pedro Costa Correia - 32795
 * @author Valter Pedro Lopes Vicente - 39360
 */
class Customer extends Thread {

    /**
     *  Nome do sistema computacional onde está localizado o servidor
     *    @serialField serverHostName
     */
    private String serverHostName = null;
    /**
     *  Número do port de escuta do servidor
     *    @serialField serverPortNumb
     */
    private int serverPortNumb;
    /**
     *  Identificação do cliente.
     *
     *    @serialField customerId
     */
    private int customerId;

    /**
     *  Instanciação do thread do cliente.
     *
     *    @param customerId identificação do cliente.
     * @param store Interface do cliente associada ao monitor da loja.
     * @param general Interface do cliente associada ao estado geral.
     */
    public Customer(int customerId, String hostName, int port) {
        super("Customer_" + customerId);

        this.customerId = customerId;
        serverHostName = hostName;
        serverPortNumb = port;
    }

    /**
     * Ciclo de vida da thread do cliente.
     */
    @Override
    public void run() {
        int ng;                              /* number of selected goods */
        while (!endOperCustomer(customerId)) {
            while (true) {
                livingNormalLife();						  /* the customer minds his own business */
                goShopping(customerId);                  /* the customer decides to visit the handicraft shop */
                if (isDoorOpen()) /* the customer checks if the shop door is open */ {
                    break;                                                    /* it is */
                } else {
                    if (isDonaIsDead()) {
                        break;
                    } else {
                        tryAgainLater();       /* it is not, the customer goes back to perform his daily chores */
                    }
                }
            }
            if (!isDonaIsDead()) {
                enterShop(customerId);        /* the customer enters the shop */

                ng = perusingAround(customerId);  /* the customer inspects the offer in display
                and eventually picks up some goods */

                if (ng != 0) {
                    addRequestCustomer(customerId);
                    iWantThis(customerId, ng);             /* the customer queues by the counter
                    to pay for the selected goods */
                }
                exitShop(customerId);                           /* the customer leaves the shop */
            }
        }
    }

    private void goShopping(int customerId) {
        ClientCom con = new ClientCom(serverHostName, serverPortNumb);
        Message inMessage, outMessage;
        while (!con.open()) // aguarda ligação
        {
            try {
                sleep((long) (10));
            } catch (InterruptedException e) {
            }
        }
        outMessage = new Message(invokesStore.goShopping,customerId);
        con.writeObject (outMessage);
        inMessage = (Message) con.readObject ();
        if (inMessage.getTypeS () != invokesStore.inShoping)
         { GenericIO.writelnString ("Thread " + getName () + ": Tipo inválido!");
           GenericIO.writelnString (inMessage.toString ());
           System.exit (1);
         }
        con.close();
    }

    private void perusingAround(int customerId){
    

    }

    /**
     *  O cliente vivendo a vida normal (operação interna).
     */
    private void livingNormalLife() {
        try {
            sleep((long) (1 + 40 * Math.random()));
        } catch (InterruptedException e) {
        }
    }

    /**
     *  O cliente volta mais tarde à store (operação interna).
     */
    private void tryAgainLater() {
        try {
            sleep((long) (1 + 10 * Math.random()));
        } catch (InterruptedException e) {
        }
    }
}
